package com.example.hosptialtag.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author liuyuhao&caiang
 * @Date 2022/2/21
 */

@Service
public class RedisService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedisTemplate <String, String> stringRedisTemplate;

    public String putSet(String key, String value) {
        redisTemplate.opsForSet().add(key, value);
        redisTemplate.expire(key,5 , TimeUnit.SECONDS);//设置过期时间，自动删除
        return "SUC";
    }
    public Set getSet(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public void deleteSet(String key, String value){
        redisTemplate.opsForSet().remove(key, value);
    }
    public boolean patientIdExist(int bind_patient_id){//判断bind_patient_id缓存中是否有以该病人id为key
        if(redisTemplate.opsForSet().size("bpiFindbti"+bind_patient_id)!=0){//"bpiFindbti"为前缀
            return true;
        }
            return false;
    }

    public boolean tagIdExist(int bind_tag_id) {//判断bind_tag_id缓存中是否有以该标签id为key
        if(redisTemplate.opsForSet().size("btiFindbpi"+bind_tag_id)!=0){//"btiFindbpi"为前缀
            return true;
        }
        return false;
    }
    public void delete(String key){
        stringRedisTemplate.delete(key);
    }
}